package com.java.common.base.dao.impl;

import java.io.Serializable;
import java.util.List;

import com.java.common.base.dao.BaseDao;
import com.java.common.base.dao.HibernateBaseDao;
import com.java.common.base.dao.QueryBuilder;
/**
 * 暴露常用的持久层操作
 * Title:DefaultBaseDao<br>
 * Description:<br>
 * Company: IBM<br>
 * Copyright @ 2012 .All rights reserved. <br>
 * @author Liuhl<br>
 * @version 2011-6-19 1.0
 */
public abstract class DefaultBaseDao<T, PK extends Serializable> extends HibernateBaseDao<T,PK> implements BaseDao<T, PK>{
	/**
	 * 单条记录增加
	 *
	 * @param model
	 * @return
	 */
	public PK save(T model){
		return super.save(model);
	}
	/**
	 * 单条记录删除
	 * @param model
	 */
	public void delete(T model){
		super.delete(model);
	}
	
	/**
	 * 单条记录更新
	 *
	 * @param model
	 */
	public void update(T model){
		super.update(model);
	}
	/**
	 * 单条记录保存或者更新
	 *
	 * @param model
	 */
	public void saveOrUpdate(T model){
		super.saveOrUpdate(model);
	}
	/**
	 * 单条记录合并
	 *
	 * @param model
	 */
	public void merge(T model){
		super.merge(model);
	}
	/**
	 * 根据主键查询单条记录
	 * @param id
	 * @return
	 */
	public T get(PK id){
		return super.get(id);
	}
	/**
	 * 根据ID批量删除
	 * @param ids
	 */
	public void delete(PK[] ids){
		super.delete(ids);
	}
	/**
	 * flush
	 */
	public void flush(){
		getSessionFactory().getCurrentSession().flush();
	}
	/**
	 * evict
	 * @param model
	 */
	public void evict(T model){
		getSessionFactory().getCurrentSession().evict(model);
	}
	
	/**
	 * 根据 排除的id集合删除对象
	 * 对象的主键必须命名为整形id
	 * @param excludeIds
	 */
	@SuppressWarnings("unchecked")
	public void deleteModelById(Integer[]excludeIds){
		String query = "from "+entityClass.getSimpleName()+" m where 1 = 1";
		QueryBuilder qb = new QueryBuilder(query);
		qb.appendCheck(" and m.id not in ", excludeIds);
        List<T> models = (List<T>) this.list(qb.getQuery().toString(),qb.getParams());
        for(T model : models){
        	delete(model);
        }
	}		
	
	
}
